<?php

namespace App\Http\Controllers\Admin\Client;

use App\Http\Controllers\Controller;
use App\Http\Requests\client\BannerRequest;
use App\Models\client\BannerModel;
use App\Models\CustomerModel;
use App\Models\CustomerTypeModel;
use Illuminate\Http\Request;

class Banner extends Controller
{
    use BannerRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $title = $request->input('title', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = BannerModel::query()->with('create_admin');

        if($title){
            $query = $query->where('title', 'like', '%'.$title.'%');
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'title' => $item->title,
                'picture_url' => $item->picture_url,
                'link_url' => $item->link_url,
                'status' => $item->status,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new BannerModel();
        $model->title = $params['title'];
        $model->picture_url = $params['picture_url'];
        $model->link_url = $params['link_url'] ?? '';
        $model->status = $params['status'];
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = BannerModel::find($id);
        if(!$model) {
            return $this->error('数据不存在');
        }

        $model->title = $params['title'];
        $model->picture_url = $params['picture_url'];
        $model->link_url = $params['link_url'];
        $model->status = $params['status'];
        $model->save();

        return $this->success();
    }

    public function delete(int $id){
        $model = BannerModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }


}
